#/bin/sh!

cat > /etc/yum.repos.d/ceph.repo << EOF
[Ceph]
name=Ceph packages for \$basearch
gpgkey=https://ceph.com/git/?p=ceph.git;a=blob_plain;f=keys/release.asc
enabled=1
baseurl=http://ceph.com/rpm-firefly/el6/\$basearch
priority=1
gpgcheck=1
type=rpm-md

[ceph-source]
name=Ceph source packages
gpgkey=https://ceph.com/git/?p=ceph.git;a=blob_plain;f=keys/release.asc
enabled=1
baseurl=http://ceph.com/rpm-firefly/el6/SRPMS
priority=1
gpgcheck=1
type=rpm-md

[Ceph-noarch]
name=Ceph noarch packages
gpgkey=https://ceph.com/git/?p=ceph.git;a=blob_plain;f=keys/release.asc
enabled=1
baseurl=http://ceph.com/rpm-firefly/el6/noarch
priority=1
gpgcheck=1
type=rpm-md


[ceph-extras]
name=Ceph Extras
baseurl=http://ceph.com/packages/ceph-extras/rpm/centos6/\$basearch
enabled=1
priority=2
gpgcheck=1
type=rpm-md
gpgkey=https://ceph.com/git/?p=ceph.git;a=blob_plain;f=keys/release.asc

[ceph-qemu-source]
name=Ceph Extras Sources
baseurl=http://ceph.com/packages/ceph-extras/rpm/centos6/SRPMS
enabled=1
priority=2
gpgcheck=1
type=rpm-md
gpgkey=https://ceph.com/git/?p=ceph.git;a=blob_plain;f=keys/release.asc
EOF

packages000="qemu-kvm qemu-img"
fileconf="/etc/nova/nova.conf"
packages="librados2 librbd1 libcephfs1 qemu-img qemu-kvm qemu-kvm-tools qemu-guest-agent qemu-guest-agent-win32 ceph openstack-nova-compute"

fileconfsave=${fileconf}.`date +%s`

cp $fileconf $fileconfsave

l=`rpm -qa $packages000 |grep ceph| wc -l`
n=`rpm -qa $packages000 | wc -l`

if [ $l -eq $n ]; then
	echo "Packages $packages000 from ceph repo. Good";
else
	echo "Packages $packages000 update to seph version";
	yum -y remove $packages000 openstack-nova-compute
fi

yum -y update
yum -y install $packages
rm -f $fileconf
cp $fileconfsave  $fileconf
chown root:nova $fileconf

service libvirtd restart


for sec in `ls /etc/libvirt/secrets/ | awk  -F"." '{print $1}' | sort -u`; do 
	virsh secret-undefine $sec
done


cp  -r  /net/192.168.200.254/scratch/scripts/ceph  /etc/
#cp  /net/192.168.200.254/scratch/scripts/ceph/ceph.client.cinder.keyring  /etc/ceph/
#chown cinder:cinder /etc/ceph/ceph.conf
user_name=admin
CEPH_ARGS="--id $user_name"
#key=`ceph auth get-key client.cinder`

export key=`grep key   /etc/ceph/ceph.client.${user_name}.keyring  |awk '{print $3}'`
echo DEBUG key=$key
fileenv=`mktemp`

#service01="openstack-cinder-backup openstack-glance-api"
service01="openstack-nova-compute libvirtd"

cat > /etc/libvirt/secret.xml << EOF
   <secret ephemeral='no' private='no'>
      <usage type='ceph'>
         <name>client.cinder secret</name>
      </usage>
   </secret>
EOF

skey=`virsh secret-define --file /etc/libvirt/secret.xml | awk '{print $2}'`
echo DEBUG: virsh secret-set-value --secret  $skey --base64 $key
virsh secret-set-value --secret  $skey --base64 $key



cat > $fileenv << EOF
libvirt   libvirt_images_type rbd
libvirt   libvirt_images_rbd_pool images
libvirt   libvirt_images_rbd_ceph_conf /etc/ceph/ceph.conf
libvirt   libvirt_inject_password false
libvirt   libvirt_inject_key false
libvirt   libvirt_inject_partition -2
libvirt   rbd_user ${user_name}
libvirt   rbd_secret_uuid $skey
EOF

cat $fileenv| while read line; do
	openstack-config --set /etc/nova/nova.conf $line
done

for serv in $service01; do
        service $serv restart;
        chkconfig $serv on;
done








